package com.example.orderflow.controller.view;

import com.example.orderflow.controller.base.ABaseController;
import com.example.orderflow.entity.constants.ApproveHistoryConstants;
import com.example.orderflow.entity.vo.ResponseVO;
import com.example.orderflow.exception.BusinessException;
import com.example.orderflow.service.ApproveHistoryService;
import com.example.orderflow.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName UserUntreatedOrdersController
 * @Description: 查看当前用户未处理的订单
 * @Since JDK 1.8
 * @Create 2024/7/12 16:07
 */

@RestController
public class UserUntreatedOrdersController extends ABaseController {

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    ApproveHistoryService approveHistoryService;

    /**
     * @create: 2024/7/12 16:07
     * @description: 查看当前用户未处理的订单
     * @param: userName 用户名
     * @return List<orderEquipment>
     */
    @PostMapping("/UserUntreatedOrders")
    public ResponseVO userUntreatedOrders(@RequestBody Map<String,String> map) {

        try {
            String userName = map.get("userName");

            //若未返回用户名，报错
            if (userName.isEmpty()){
                throw new BusinessException("用户名错误");
            }

            //若用户名未找到相关订单，返回空
            if (approveHistoryService.findOrderIdsByApproveByAndApproveStatus(userName, ApproveHistoryConstants.APPROVE_STATUS_INREVIEW).isEmpty()){
                return getSuccessResponseVO(null);
            }

            //根据用户名和审核状态批量查找订单号
            List<String> orderIds = approveHistoryService.findOrderIdsByApproveByAndApproveStatus(userName, ApproveHistoryConstants.APPROVE_STATUS_INREVIEW);

            //再根据订单号批量返回订单信息
            return getSuccessResponseVO(orderInfoService.getOrderInfoByOrderIds(orderIds));

        }catch (Exception e){
            throw new BusinessException("系统异常，请联系管理员处理");
        }

    }

}
